package com.marui.ch2.tools;

import java.util.*;
import java.util.concurrent.Exchanger;

/**
 *@author marui
 *
 *类说明：Exchange的使用
 */
public class UseExchange {
    private static final Exchanger<Set<String>> exchange 
    	= new Exchanger<Set<String>>();

    public static void main(String[] args) {

    	//第一个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
            	Set<String> setA = new HashSet<String>();//存放数据的容器
                try {
                	/*添加数据
                	 * set.add(.....)
                	 * */
                    setA.add("first1");
                	setA = exchange.exchange(setA);//交换set
                	/*处理交换后的数据*/
                    Iterator<String> iterator = setA.iterator();
                    while (iterator.hasNext()){
                        String next = iterator.next();
                        System.out.println("A:"+next);
                    }
                } catch (InterruptedException e) {
                }
            }
        }).start();

      //第二个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
            	Set<String> setB = new HashSet<String>();//存放数据的容器
                try {
                	/*添加数据
                	 * set.add(.....)
                	 * set.add(.....)
                	 * */
                	setB.add("second1");
                	setB = exchange.exchange(setB);//交换set
                    Iterator<String> iterator = setB.iterator();
                    while (iterator.hasNext()){
                        String next = iterator.next();
                        System.out.println("B:"+next);
                    }
                    /*处理交换后的数据*/
                } catch (InterruptedException e) {
                }
            }
        }).start();

        //第三个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                Set<String> setC = new HashSet<String>();//存放数据的容器
                try {
                    /*添加数据
                     * set.add(.....)
                     * set.add(.....)
                     * */
                    setC.add("third1");
                    setC = exchange.exchange(setC);//交换set
                    Iterator<String> iterator = setC.iterator();
                    while (iterator.hasNext()){
                        String next = iterator.next();
                        System.out.println("C:"+next);
                    }
                    /*处理交换后的数据*/
                } catch (InterruptedException e) {
                }
            }
        }).start();

        //第四个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                Set<String> setD = new HashSet<String>();//存放数据的容器
                try {
                    /*添加数据
                     * set.add(.....)
                     * set.add(.....)
                     * */
                    setD.add("fourth1");
                    setD = exchange.exchange(setD);//交换set
                    Iterator<String> iterator = setD.iterator();
                    while (iterator.hasNext()){
                        String next = iterator.next();
                        System.out.println("D:"+next);
                    }
                    /*处理交换后的数据*/
                } catch (InterruptedException e) {
                }
            }
        }).start();
    }
}
